
Namespace Validation
    <AttributeUsage(AttributeTargets.Property, AllowMultiple:=True, Inherited:=False)> Public NotInheritable Class CompareValueValidatorAttribute
        Inherits BaseValidatorAttribute

#Region " Declarations "

        Private _enumComparisonType As ComparisonType = ComparisonType.Equal
        Private _enumRequiredEntry As RequiredEntry = RequiredEntry.No
        Private _objCompareToValue As IComparable

#End Region

#Region " Properties "

        Public ReadOnly Property CompareToValue() As IComparable
            Get
                Return _objCompareToValue
            End Get
        End Property

        Public ReadOnly Property ComparisonType() As ComparisonType
            Get
                Return _enumComparisonType
            End Get
        End Property

        Public ReadOnly Property RequiredEntry() As RequiredEntry
            Get
                Return _enumRequiredEntry
            End Get
        End Property

#End Region

#Region " Constructor "

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As Double, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As Integer, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As Long, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As Short, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As Single, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As String, ByVal enumRequiredEntry As RequiredEntry)
            _enumComparisonType = enumComparisonType
            _objCompareToValue = objCompareToValue
            _enumRequiredEntry = enumRequiredEntry
        End Sub

        ''' <summary>
        '''HACK - New Feature - fixed constructor to support date and decimal types since these are not allowed in a attributes constructor
        '''There is some restriction on the data types that can be used as attribute parameters. 
        '''Parameters can be any integral data type (Byte, Short, Integer, Long) or floating point data type (Single and Double), as well as Char, String, Boolean, an enumerated type, or System.Type. 
        '''Thus, Date, Decimal, Object, and structured types cannot be used as parameters.
        '''This is a simple work around.
        ''' </summary>
        Public Sub New(ByVal enumComparisonType As ComparisonType, ByVal objCompareToValue As String, ByVal enumRequiredEntry As RequiredEntry, ByVal enumConvertToType As ConvertToType)
            _enumComparisonType = enumComparisonType

            Select Case enumConvertToType

                Case ConvertToType.Date
                    _objCompareToValue = CType(objCompareToValue, Date)

                Case ConvertToType.Decimal
                    _objCompareToValue = CType(objCompareToValue, Decimal)

                Case Else
                    Throw New ArgumentOutOfRangeException("enumConvertToType", "This ConverToType has not yet been programmed, value passed was: " & enumConvertToType.ToString)
            End Select

            _enumRequiredEntry = enumRequiredEntry
        End Sub

#End Region

#Region " Methods "

        Public Overrides Function Create(ByVal strPropertyName As String) As Validator
            Return New Validator(AddressOf Validation.ComparisonValidationRules.CompareValueRule, New CompareValueRuleEventArgs(Me, strPropertyName), RuleType.Attribute)
        End Function

#End Region

    End Class

End Namespace
